package JDIC;

import Utils.MSJ_JOptionPane;
import java.awt.*;

/**
 * <b>Use this class to create Tray icon for your project.</b><br> JDIC : Java
 * Desktop Integration Components.
 *
 * @author Mohamed Tarek Salama
 * @version 1.1 BETA
 */
public final class MSJ_TrayIcon {

    TrayIcon trayIcon;

    /**
     * Make Tray icon with popupmenu<br> ! <i>Remember that you will use
     * <b>PopupMenu</b> not <b>JPopupMenu</b></i><br> ! <i>Remember : make sure
     * that the image path is a correct String path</i><br>
     *
     * @param imagepath : String
     * @param traytooltip : String
     * @param popm : PopUpMenu
     */
    public void MakeTray(String imagepath, String traytooltip, PopupMenu popm) {
        Image image = null;

        if (SystemTray.isSupported()) {
            // get the SystemTray instance
            SystemTray systray = SystemTray.getSystemTray();
            // load an image
            try {
                Toolkit t = Toolkit.getDefaultToolkit();
                image = t.getImage(this.getClass().getResource(imagepath));
            } catch (Exception ex) {
                MSJ_JOptionPane.show_Error_Message("Error invoke image from location\n" + ex.toString());
            }
            trayIcon = new TrayIcon(image, traytooltip, popm);

            try {
                systray.add(trayIcon);
            } catch (AWTException ex) {
                MSJ_JOptionPane.show_Error_Message("Error making Icon Tray\n" + ex.toString());
            }
        } else {
            MSJ_JOptionPane.show_Error_Message("System Tray not Supported");
        }
    }

    /**
     * Make Tray icon without popupmenu<br> ! <i>Remember : make sure that the
     * image path is a correct String path</i><br>
     *
     * @param imagepath : String
     */
    public void MakeTray(String imagepath) {
        Image image;

        if (SystemTray.isSupported()) {
            // get the SystemTray instance
            SystemTray systray = SystemTray.getSystemTray();
            // load an image
            Toolkit t = Toolkit.getDefaultToolkit();
            image = t.getImage(this.getClass().getResource(imagepath));
            //define trayicon image
            trayIcon = new TrayIcon(image);
            try {
                //add tray icon to the system
                systray.add(trayIcon);
            } catch (AWTException ex) {
                MSJ_JOptionPane.show_Error_Message("Error making Icon Tray\n" + ex.toString());
            }
        } else {
            MSJ_JOptionPane.show_Error_Message("System Tray not Supported");
        }
    }

    /**
     * Change Tray icon <b>image</b> with a new image<br> ! <i>Remember : make
     * sure that the image path is a correct String path</i><br>
     *
     * @param updatedimagepath : String
     */
    public void changeTrayimage(String updatedimagepath) {
        if (SystemTray.isSupported()) {
            if (trayIcon != null) {
                trayIcon.setImage(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource(updatedimagepath)));
            } else {
                MSJ_JOptionPane.show_Error_Message("Error \n You must invoke MakeTray() first");
            }
        } else {
            MSJ_JOptionPane.show_Error_Message("System Tray not Supported");
        }
    }
}
